{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import warnings\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Data Understanding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of engines: 260\n"
     ]
    }
   ],
   "source": [
    "data_training_FD2 = pd.read_csv('train_FD002.txt',sep=\" \", header=None)\n",
    "data_training_FD2.head()\n",
    "engine_cycle = ['engine_id','cycle']\n",
    "settings = ['setting1','setting2','setting3']\n",
    "sensors = ['s%s'%i for i in range(1,22)]\n",
    "nan_cols = ['NaN1','NaN2']\n",
    "data_training_FD2.columns = engine_cycle + settings + sensors + nan_cols\n",
    "data_training_FD2 = data_training_FD2.drop(['NaN1','NaN2'],axis=1)\n",
    "\n",
    "# check number of units\n",
    "print 'Number of engines:', data_training_FD2['engine_id'].unique().shape[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Time of Event for particular engine = number of last cycle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# suppress warning message\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "data_training_FD2['time_of_event']=np.nan\n",
    "for eng in data_training_FD2.engine_id.value_counts().index:\n",
    "    data_training_FD2['time_of_event'][data_training_FD2.engine_id==eng] = max(data_training_FD2.cycle[data_training_FD2.engine_id==eng])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Time to Event (RUL) for particular engine = number of last cycle - number of present cycle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>engine_id</th>\n",
       "      <th>cycle</th>\n",
       "      <th>setting1</th>\n",
       "      <th>setting2</th>\n",
       "      <th>setting3</th>\n",
       "      <th>s1</th>\n",
       "      <th>s2</th>\n",
       "      <th>s3</th>\n",
       "      <th>s4</th>\n",
       "      <th>s5</th>\n",
       "      <th>...</th>\n",
       "      <th>s14</th>\n",
       "      <th>s15</th>\n",
       "      <th>s16</th>\n",
       "      <th>s17</th>\n",
       "      <th>s18</th>\n",
       "      <th>s19</th>\n",
       "      <th>s20</th>\n",
       "      <th>s21</th>\n",
       "      <th>time_of_event</th>\n",
       "      <th>time_to_event</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>34.9983</td>\n",
       "      <td>0.8400</td>\n",
       "      <td>100.0</td>\n",
       "      <td>449.44</td>\n",
       "      <td>555.32</td>\n",
       "      <td>1358.61</td>\n",
       "      <td>1137.23</td>\n",
       "      <td>5.48</td>\n",
       "      <td>...</td>\n",
       "      <td>8048.56</td>\n",
       "      <td>9.3461</td>\n",
       "      <td>0.02</td>\n",
       "      <td>334</td>\n",
       "      <td>2223</td>\n",
       "      <td>100.00</td>\n",
       "      <td>14.73</td>\n",
       "      <td>8.8071</td>\n",
       "      <td>149.0</td>\n",
       "      <td>148.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>41.9982</td>\n",
       "      <td>0.8408</td>\n",
       "      <td>100.0</td>\n",
       "      <td>445.00</td>\n",
       "      <td>549.90</td>\n",
       "      <td>1353.22</td>\n",
       "      <td>1125.78</td>\n",
       "      <td>3.91</td>\n",
       "      <td>...</td>\n",
       "      <td>8072.30</td>\n",
       "      <td>9.3774</td>\n",
       "      <td>0.02</td>\n",
       "      <td>330</td>\n",
       "      <td>2212</td>\n",
       "      <td>100.00</td>\n",
       "      <td>10.41</td>\n",
       "      <td>6.2665</td>\n",
       "      <td>149.0</td>\n",
       "      <td>147.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>24.9988</td>\n",
       "      <td>0.6218</td>\n",
       "      <td>60.0</td>\n",
       "      <td>462.54</td>\n",
       "      <td>537.31</td>\n",
       "      <td>1256.76</td>\n",
       "      <td>1047.45</td>\n",
       "      <td>7.05</td>\n",
       "      <td>...</td>\n",
       "      <td>7864.87</td>\n",
       "      <td>10.8941</td>\n",
       "      <td>0.02</td>\n",
       "      <td>309</td>\n",
       "      <td>1915</td>\n",
       "      <td>84.93</td>\n",
       "      <td>14.08</td>\n",
       "      <td>8.6723</td>\n",
       "      <td>149.0</td>\n",
       "      <td>146.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>42.0077</td>\n",
       "      <td>0.8416</td>\n",
       "      <td>100.0</td>\n",
       "      <td>445.00</td>\n",
       "      <td>549.51</td>\n",
       "      <td>1354.03</td>\n",
       "      <td>1126.38</td>\n",
       "      <td>3.91</td>\n",
       "      <td>...</td>\n",
       "      <td>8068.66</td>\n",
       "      <td>9.3528</td>\n",
       "      <td>0.02</td>\n",
       "      <td>329</td>\n",
       "      <td>2212</td>\n",
       "      <td>100.00</td>\n",
       "      <td>10.59</td>\n",
       "      <td>6.4701</td>\n",
       "      <td>149.0</td>\n",
       "      <td>145.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>25.0005</td>\n",
       "      <td>0.6203</td>\n",
       "      <td>60.0</td>\n",
       "      <td>462.54</td>\n",
       "      <td>537.07</td>\n",
       "      <td>1257.71</td>\n",
       "      <td>1047.93</td>\n",
       "      <td>7.05</td>\n",
       "      <td>...</td>\n",
       "      <td>7861.23</td>\n",
       "      <td>10.8963</td>\n",
       "      <td>0.02</td>\n",
       "      <td>309</td>\n",
       "      <td>1915</td>\n",
       "      <td>84.93</td>\n",
       "      <td>14.13</td>\n",
       "      <td>8.5286</td>\n",
       "      <td>149.0</td>\n",
       "      <td>144.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   engine_id  cycle  setting1  setting2  setting3      s1      s2       s3  \\\n",
       "0          1      1   34.9983    0.8400     100.0  449.44  555.32  1358.61   \n",
       "1          1      2   41.9982    0.8408     100.0  445.00  549.90  1353.22   \n",
       "2          1      3   24.9988    0.6218      60.0  462.54  537.31  1256.76   \n",
       "3          1      4   42.0077    0.8416     100.0  445.00  549.51  1354.03   \n",
       "4          1      5   25.0005    0.6203      60.0  462.54  537.07  1257.71   \n",
       "\n",
       "        s4    s5      ...            s14      s15   s16  s17   s18     s19  \\\n",
       "0  1137.23  5.48      ...        8048.56   9.3461  0.02  334  2223  100.00   \n",
       "1  1125.78  3.91      ...        8072.30   9.3774  0.02  330  2212  100.00   \n",
       "2  1047.45  7.05      ...        7864.87  10.8941  0.02  309  1915   84.93   \n",
       "3  1126.38  3.91      ...        8068.66   9.3528  0.02  329  2212  100.00   \n",
       "4  1047.93  7.05      ...        7861.23  10.8963  0.02  309  1915   84.93   \n",
       "\n",
       "     s20     s21  time_of_event  time_to_event  \n",
       "0  14.73  8.8071          149.0          148.0  \n",
       "1  10.41  6.2665          149.0          147.0  \n",
       "2  14.08  8.6723          149.0          146.0  \n",
       "3  10.59  6.4701          149.0          145.0  \n",
       "4  14.13  8.5286          149.0          144.0  \n",
       "\n",
       "[5 rows x 28 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_training_FD2['time_to_event'] = data_training_FD2['time_of_event'] - data_training_FD2['cycle']\n",
    "data_training_FD2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Input Centering and Target Normalization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "data_training = data_training_FD2.copy()\n",
    "settings_min = data_training_FD2[settings].min()\n",
    "settings_max = data_training_FD2[settings].max()\n",
    "settings_mean = data_training_FD2[settings].mean()\n",
    "for setting in settings:\n",
    "    data_training[setting] = (data_training_FD2[setting] - settings_mean[setting])/(settings_max[setting] - settings_min[setting])\n",
    "    \n",
    "sensors_min = data_training_FD2[sensors].min()\n",
    "sensors_max = data_training_FD2[sensors].max()\n",
    "sensors_mean = data_training_FD2[sensors].mean()\n",
    "for sensor in sensors:\n",
    "    data_training[sensor] = (data_training_FD2[sensor] - sensors_mean[sensor])/(sensors_max[sensor] - sensors_min[sensor])\n",
    "    \n",
    "time_of_event_max = data_training_FD2['time_of_event'].max()\n",
    "data_training['time_to_event'] = data_training_FD2['time_to_event']/time_of_event_max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>engine_id</th>\n",
       "      <th>cycle</th>\n",
       "      <th>setting1</th>\n",
       "      <th>setting2</th>\n",
       "      <th>setting3</th>\n",
       "      <th>s1</th>\n",
       "      <th>s2</th>\n",
       "      <th>s3</th>\n",
       "      <th>s4</th>\n",
       "      <th>s5</th>\n",
       "      <th>...</th>\n",
       "      <th>s14</th>\n",
       "      <th>s15</th>\n",
       "      <th>s16</th>\n",
       "      <th>s17</th>\n",
       "      <th>s18</th>\n",
       "      <th>s19</th>\n",
       "      <th>s20</th>\n",
       "      <th>s21</th>\n",
       "      <th>time_of_event</th>\n",
       "      <th>time_to_event</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.261852</td>\n",
       "      <td>0.318223</td>\n",
       "      <td>0.148849</td>\n",
       "      <td>-0.318586</td>\n",
       "      <td>-0.223437</td>\n",
       "      <td>-0.166222</td>\n",
       "      <td>-0.164184</td>\n",
       "      <td>-0.238281</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.042933</td>\n",
       "      <td>0.006022</td>\n",
       "      <td>-0.332595</td>\n",
       "      <td>-0.149057</td>\n",
       "      <td>-0.012276</td>\n",
       "      <td>0.148849</td>\n",
       "      <td>-0.207795</td>\n",
       "      <td>-0.208556</td>\n",
       "      <td>149.0</td>\n",
       "      <td>0.391534</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.428485</td>\n",
       "      <td>0.319173</td>\n",
       "      <td>0.148849</td>\n",
       "      <td>-0.378854</td>\n",
       "      <td>-0.273166</td>\n",
       "      <td>-0.180824</td>\n",
       "      <td>-0.191744</td>\n",
       "      <td>-0.384873</td>\n",
       "      <td>...</td>\n",
       "      <td>0.013572</td>\n",
       "      <td>0.017482</td>\n",
       "      <td>-0.332595</td>\n",
       "      <td>-0.190724</td>\n",
       "      <td>-0.035531</td>\n",
       "      <td>0.148849</td>\n",
       "      <td>-0.355943</td>\n",
       "      <td>-0.353075</td>\n",
       "      <td>149.0</td>\n",
       "      <td>0.388889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.023814</td>\n",
       "      <td>0.059078</td>\n",
       "      <td>-0.851151</td>\n",
       "      <td>-0.140766</td>\n",
       "      <td>-0.388682</td>\n",
       "      <td>-0.442127</td>\n",
       "      <td>-0.380282</td>\n",
       "      <td>-0.091689</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.480144</td>\n",
       "      <td>0.572806</td>\n",
       "      <td>-0.332595</td>\n",
       "      <td>-0.409474</td>\n",
       "      <td>-0.663438</td>\n",
       "      <td>-0.851151</td>\n",
       "      <td>-0.230086</td>\n",
       "      <td>-0.216224</td>\n",
       "      <td>149.0</td>\n",
       "      <td>0.386243</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0.428711</td>\n",
       "      <td>0.320123</td>\n",
       "      <td>0.148849</td>\n",
       "      <td>-0.378854</td>\n",
       "      <td>-0.276745</td>\n",
       "      <td>-0.178629</td>\n",
       "      <td>-0.190300</td>\n",
       "      <td>-0.384873</td>\n",
       "      <td>...</td>\n",
       "      <td>0.004909</td>\n",
       "      <td>0.008475</td>\n",
       "      <td>-0.332595</td>\n",
       "      <td>-0.201141</td>\n",
       "      <td>-0.035531</td>\n",
       "      <td>0.148849</td>\n",
       "      <td>-0.349770</td>\n",
       "      <td>-0.341494</td>\n",
       "      <td>149.0</td>\n",
       "      <td>0.383598</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>0.023855</td>\n",
       "      <td>0.057296</td>\n",
       "      <td>-0.851151</td>\n",
       "      <td>-0.140766</td>\n",
       "      <td>-0.390884</td>\n",
       "      <td>-0.439553</td>\n",
       "      <td>-0.379127</td>\n",
       "      <td>-0.091689</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.488808</td>\n",
       "      <td>0.573611</td>\n",
       "      <td>-0.332595</td>\n",
       "      <td>-0.409474</td>\n",
       "      <td>-0.663438</td>\n",
       "      <td>-0.851151</td>\n",
       "      <td>-0.228371</td>\n",
       "      <td>-0.224398</td>\n",
       "      <td>149.0</td>\n",
       "      <td>0.380952</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   engine_id  cycle  setting1  setting2  setting3        s1        s2  \\\n",
       "0          1      1  0.261852  0.318223  0.148849 -0.318586 -0.223437   \n",
       "1          1      2  0.428485  0.319173  0.148849 -0.378854 -0.273166   \n",
       "2          1      3  0.023814  0.059078 -0.851151 -0.140766 -0.388682   \n",
       "3          1      4  0.428711  0.320123  0.148849 -0.378854 -0.276745   \n",
       "4          1      5  0.023855  0.057296 -0.851151 -0.140766 -0.390884   \n",
       "\n",
       "         s3        s4        s5      ...             s14       s15       s16  \\\n",
       "0 -0.166222 -0.164184 -0.238281      ...       -0.042933  0.006022 -0.332595   \n",
       "1 -0.180824 -0.191744 -0.384873      ...        0.013572  0.017482 -0.332595   \n",
       "2 -0.442127 -0.380282 -0.091689      ...       -0.480144  0.572806 -0.332595   \n",
       "3 -0.178629 -0.190300 -0.384873      ...        0.004909  0.008475 -0.332595   \n",
       "4 -0.439553 -0.379127 -0.091689      ...       -0.488808  0.573611 -0.332595   \n",
       "\n",
       "        s17       s18       s19       s20       s21  time_of_event  \\\n",
       "0 -0.149057 -0.012276  0.148849 -0.207795 -0.208556          149.0   \n",
       "1 -0.190724 -0.035531  0.148849 -0.355943 -0.353075          149.0   \n",
       "2 -0.409474 -0.663438 -0.851151 -0.230086 -0.216224          149.0   \n",
       "3 -0.201141 -0.035531  0.148849 -0.349770 -0.341494          149.0   \n",
       "4 -0.409474 -0.663438 -0.851151 -0.228371 -0.224398          149.0   \n",
       "\n",
       "   time_to_event  \n",
       "0       0.391534  \n",
       "1       0.388889  \n",
       "2       0.386243  \n",
       "3       0.383598  \n",
       "4       0.380952  \n",
       "\n",
       "[5 rows x 28 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_training.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Set a look-back time range to train the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "dt = 30"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Prepare the training data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "Xtraining, Ytraining = [], []\n",
    "for engine in data_training['engine_id'].unique():\n",
    "    #print engine, X[X['engine_id'] == engine]['cycle'].max()\n",
    "    cycle_max = data_training[data_training['engine_id'] == engine]['cycle'].max()\n",
    "    for i in range(cycle_max - dt + 1):\n",
    "        select_Xdata = data_training[data_training['engine_id'] == engine][settings+sensors][i:i+dt].as_matrix()\n",
    "        Xtraining.append(select_Xdata)\n",
    "        select_Ydata = data_training[data_training['engine_id'] == engine]['time_to_event'].iloc[i+dt-1]\n",
    "        Ytraining.append(select_Ydata)\n",
    "Xtraining = np.array(Xtraining)\n",
    "Ytraining = np.array(Ytraining)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Prepare training data for testing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Xtraining_to_testing, Ytraining_to_testing, engines_training_to_testing = [], [], []\n",
    "for engine in data_training['engine_id'].unique():\n",
    "    Xtest_engine, Ytest_engine = [], []\n",
    "    cycle_max = data_training[data_training['engine_id'] == engine]['cycle'].max()\n",
    "    if cycle_max > (dt+5): # at least 5 samples from the engine are expected\n",
    "        for i in range(cycle_max - dt + 1):\n",
    "            select_Xdata = data_training[data_training['engine_id'] == engine][settings+sensors][i:i+dt].as_matrix()\n",
    "            Xtest_engine.append(select_Xdata)\n",
    "            select_Ydata = data_training[data_training['engine_id'] == engine]['time_to_event'].iloc[i+dt-1]\n",
    "            Ytest_engine.append(select_Ydata)\n",
    "        Xtraining_to_testing.append(np.array(Xtest_engine))\n",
    "        Ytraining_to_testing.append(np.array(Ytest_engine))\n",
    "        engines_training_to_testing.append(engine)\n",
    "Xtraining_to_testing = np.array(Xtraining_to_testing)\n",
    "Ytraining_to_testing = np.array(Ytraining_to_testing)\n",
    "engines_training_to_testing = np.array(engines_training_to_testing)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Save training data and its testing data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "print Xtraining.shape, Ytraining.shape\n",
    "np.save('Xtraining_data_dt_30', Xtraining) # Xtraining = np.load('Xtraining_data_dt_30.npy')\n",
    "np.save('Ytraining_data_dt_30', Ytraining) # Ytraining = np.load('Ytraining_data_dt_30.npy')\n",
    "\n",
    "np.save('Xtraining_to_testing_dt_30',Xtraining_to_testing)\n",
    "np.save('Ytraining_to_testing_dt_30',Ytraining_to_testing)\n",
    "np.save('engines_training_to_testing_dt_30',engines_training_to_testing)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Modeling\n",
    "#### Import modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "from keras.models import Sequential\n",
    "from keras.layers import LSTM\n",
    "from keras.layers import Dense"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Design RNN model architecture"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "model = Sequential()\n",
    "model.add(LSTM(32, return_sequences = True, input_shape = (dt,len(settings+sensors))))\n",
    "model.add(LSTM(64))\n",
    "model.add(Dense(8))\n",
    "model.add(Dense(8))\n",
    "model.add(Dense(1))\n",
    "model.compile(loss='mae',optimizer='adam')\n",
    "print model.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "#### Model training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#from keras.models import load_model\n",
    "#model = load_model('model_dt_30.h5')\n",
    "history = model.fit(Xtraining,Ytraining,epochs=500,batch_size=32)\n",
    "# save the model\n",
    "model.save('model_dt_30.h5')\n",
    "mae_loss = pd.DataFrame(history.history['loss'],columns = ['loss'])\n",
    "# save the loss\n",
    "mae_loss.to_csv('mae_loss_dt_30.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# 0.0764"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
